﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;

using Bpms.Data;
using Bpms.Entity;
using Bpms.Log;
using Bpms.Common;

namespace Bpms.Data.SqlServer
{
    public partial class DataProvider 
    {
        #region Private

        string contactClassName = "Bpms.Data.SqlServer.Contact";

        /// <summary>
        /// 绑定数据到联系人实体
        /// </summary>
        /// <param name="idr">idatareader</param>
        /// <returns>IList<ContactInfo>对象</returns>
        IList<ContactInfo> GetDataBindedContact(IDataReader idr)
        {
            IList<ContactInfo> contactList = new List<ContactInfo>();

            while (idr.Read())
            {
                ContactInfo contact = new ContactInfo();
                contact.ContactID = (int)idr["contact_id"];             // 标识ID（联系人ID）
                contact.EnName = idr["en_name"].ToString();             // 英文名
                contact.ChName = idr["ch_name"].ToString();             // 中文名
                contact.Gender = (byte)idr["gender"];                   // 性别（1：男，2：女）
                contact.CustomerID = (int)idr["customer_id"];           // 所属公司（客户ID）
                contact.Branch = idr["branch"].ToString();              // 分公司/集团
                contact.Department = idr["department"].ToString();      // 项目组/部门
                contact.Edm = idr["edm"].ToString();                    // EDM
                contact.Status = (byte)idr["status"];                   // 客户状态（1：在职，2：离职）
                contact.Type = (byte)idr["type"];                       // 客户公司类型（1：甲方，2：乙方（广告、公关等））
                contact.Nationality = (byte)idr["nationality"];         // 客户联系人国籍(1：Chinese，2：Foreigner)
                contact.UserID = (int)idr["user_id"];                   // 客服人员；用户ID
                contact.Telephone = idr["telephone"].ToString();        // 座机电话
                contact.Mobile = idr["mobile"].ToString();              // 手机
                contact.Email = idr["email"].ToString();                // E-mail
                contact.AreaID = (int)idr["area_id"];                   // 所在地区ID
                contact.Address = idr["address"].ToString();            // 通信地址
                contact.GeneralJobTitleID = (int)idr["general_job_title_id"];   // 职务，通用职务ID
                contact.Fax = idr["fax"].ToString();                    // 传真
                contact.Remark = idr["remark"].ToString();              // 备注
                contact.AddTime = TimeFormat.Time_T2DateTime(Convert.ToInt64(idr["add_time"]));     // 添加时间
                contact.IsDelete = Convert.ToBoolean(idr["is_delete"]);              // 是否删除（false：否，true：是）

                contactList.Add(contact);
            }

            return contactList;
        }

        /// <summary>
        /// 根据sql语句返回联系人信息
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <returns>联系人对象</returns>
        ContactInfo GetContactBySQL(string sql)
        {
            IList<ContactInfo> contactList = GetContactListBySQL(sql);

            if (contactList.Count > 0)
            {
                return contactList[0];
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 根据sql语句返回联系人列表
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <returns> IList<ContactInfo>对象</returns>
        IList<ContactInfo> GetContactListBySQL(string sql)
        {
            IList<ContactInfo> contactList = new List<ContactInfo>();

            using (IDataReader idr = DbHelper.ExecuteReader(CommandType.Text, sql) as IDataReader)
            {
                try
                {
                    contactList = GetDataBindedContact(idr);
                }
                catch (Exception e)
                {
                    log.WriteLog(this.contactClassName + "：获取绑定的联系人信息时出现异常", e);
                }
                finally
                {
                    if (idr != null)
                    {
                        idr.Close();
                    }
                }
            }

            return contactList;
        }

        #endregion

        /// <summary>
        /// 添加联系人
        /// </summary>
        /// <param name="contact">联系人实体</param>
        /// <returns>添加是否成功（true：成功，false：失败）</returns>
        public bool InsertContact(ContactInfo contact)
        {
            return true;
        }

        /// <summary>
        /// 修改联系人
        /// </summary>
        /// <param name="contact">联系人实体</param>
        /// <returns>修改是否成功（true：成功，false：失败）</returns>
        public bool UpdateContact(ContactInfo contact)
        {
            return true;
        }

        /// <summary>
        /// 删除联系人
        /// </summary>
        /// <param name="contactId">联系人ID</param>
        /// <returns>删除是否成功（true：成功，false：失败）</returns>
        public bool DeleteContact(int contactId)
        {
            return true;
        }

        //public bool ContactExists(ContactInfo contact, OperateType type)
        //{
        //    return true;
        //}

        /// <summary>
        /// 得到联系人信息
        /// </summary>
        /// <param name="contactId">联系人ID</param>
        /// <returns>联系人信息</returns>
        public ContactInfo GetContact(int contactId)
        {
            return null;
        }

        /// <summary>
        /// 得到联系人列表
        /// </summary>
        /// <param name="pageId">当前页ID</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="condition">提取的条件</param>
        /// <returns>联系人列表</returns>
        public IList<ContactInfo> GetContacts(int pageId, int pageSize, string condition)
        {
            return null;
        }

        /// <summary>
        /// 得到联系人列表
        /// </summary>
        /// <param name="condition">提取的条件</param>
        /// <returns>联系人列表</returns>
        public IList<ContactInfo> GetContacts(string condition)
        {
            StringBuilder sbSql = new StringBuilder();

            sbSql.Append(" SELECT contact_id, en_name, ch_name, gender, customer_id, branch, department, edm, ");
            sbSql.Append(" status, type, nationality, [user_id], telephone, mobile, email, area_id, address, ");
            sbSql.Append(" general_job_title_id, fax, remark, add_time, is_delete ");
            sbSql.Append(" FROM bp_contact ");
            sbSql.Append(" WHERE is_delete = 0 ");

            if (condition.Trim() != "")
            {
                sbSql.Append(" AND ").Append(condition);
            }
            sbSql.Append(" ORDER BY contact_id ASC ");

            try
            {
                return GetContactListBySQL(sbSql.ToString());
            }
            catch (Exception e)
            {
                log.WriteLog(this.customerClassName + "：得到满足条件的联系人时出现异常", e);
                return null;
            }
        }
    }
}
